package com.zhentao.service.impl;

import com.zhentao.config.MinioConfig;
import com.zhentao.config.MinioConfigProperties;
import com.zhentao.service.FileStorageService;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Import;

import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

@EnableConfigurationProperties(MinioConfigProperties.class)
@Import(MinioConfig.class)

public class FileStorageServiceImpl implements FileStorageService {
    @Autowired
    private MinioClient minioClient;//minio客户端
    @Autowired
    private MinioConfigProperties minioConfigProperties;//minio配置

    @Override
    public String uploadHtmlFile(String fileName, InputStream inputStream)
            throws IOException, ServerException, InvalidBucketNameException,
            InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException,
            InvalidKeyException, InvalidResponseException, XmlParserException,
            InternalException {
        //文件名的处理
        fileName = UUID.randomUUID().toString().replace("-", "").substring(0, 6) + ".png";
        //文件的上传操作
        PutObjectArgs putObjectArgs = PutObjectArgs.builder()//创建上传的参数
                .object(fileName)//文件名
                .bucket(minioConfigProperties.getBucket())//桶
                .contentType("text/html")//文件类型
                .stream(inputStream, inputStream.available(), -1)//文件流
                .build();
        minioClient.putObject(putObjectArgs);//上传
        //url
        String path = minioClient.getObjectUrl(minioConfigProperties.getBucket(), putObjectArgs.object());//获取url
        System.out.println(path);
        return path;
    }

    @Override
    public String uploadImgFile(String fileName, InputStream inputStream) throws IOException, ServerException, InvalidBucketNameException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        // 验证输入流不为空
        if (inputStream == null) {
            throw new IllegalArgumentException("Input stream cannot be null");
        }

        // 生成随机文件名，保留原始文件扩展名
        String originalExtension = fileName.substring(fileName.lastIndexOf("."));
        String newFileName = UUID.randomUUID().toString().replace("-", "").substring(0, 6) + originalExtension;
        // 根据文件扩展名确定contentType
        String contentType = determineContentType(originalExtension);

        try {
                PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                        .object(newFileName)
                        .bucket(minioConfigProperties.getBucket())
                        .contentType(contentType)
                        .stream(inputStream, inputStream.available(), -1)
                        .build();

                minioClient.putObject(putObjectArgs);//上传

                // 获取URL
                return minioClient.getObjectUrl(minioConfigProperties.getBucket(), newFileName);
            // 文件上传操作
        } finally {
            // 确保输入流关闭
            inputStream.close();
        }
    }

    private String determineContentType(String fileExtension) {
        switch (fileExtension.toLowerCase()) {
            case ".png":
                return "image/png";
            case ".jpg":
            case ".jpeg":
                return "image/jpeg";
            case ".gif":
                return "image/gif";
            case ".bmp":
                return "image/bmp";
            case ".webp":
                return "image/webp";
            default:
                return "application.yml/octet-stream";
        }
    }
}

